// File:     Blob.cpp
//
// Modified: 2011 Jan. 27
//
// 
//

#include "Blob.hpp"


Blob::Blob(char* imgName)
// @des:
// @input:
// @output:
{
	this ->setImg(imgName);
	this ->hsv_ = cvCreateImage(cvGetSize(this ->img_), IPL_DEPTH_8U, 3);
	this ->h_ = cvCreateImage(cvGetSize(this ->img_), IPL_DEPTH_8U, 1);
}

Blob::~Blob()
// @des:
// @input:
// @output:
{
	cvReleaseImage(&(this ->img_));
	cvReleaseImage(&(this ->hsv_));
	cvReleaseImage(&(this ->h_));
}

void Blob::setHSV()
// @des:
// @input:
// @output:
{
	if (!this ->img_) fprintf(stderr, "Image object is null");

	cvCvtColor(this ->img_, this ->hsv_, CV_BGR2HSV);
}

void Blob::setImg(const char* c)
// @des:
// @input:
// @output:
{
	this ->img_ = cvLoadImage(c);
}


void Blob::setHue()
// @des:
// @input:
// @output:
{
	if(!this ->hsv_ || !this ->img_)
	{
		fprintf(stderr, "Image channel is null");
	}

	IplImage* dummy = cvCreateImage(cvGetSize(this ->img_), IPL_DEPTH_8U, 1);	// The dummy is place holder for saturation and value channels, which at this point isn't used in our algorithm

	cvSplit(this ->hsv_, this ->h_, dummy, dummy, NULL);

	
	cvThreshold(this ->h_, this ->h_, 145, 256, CV_THRESH_BINARY);	// Creates the binary image, all colors w/ hue 40 or above = white, everyone else = black
	cvReleaseImage(&dummy);
}

void Blob::setKernel(int cols, int rows, int x_coord, int y_coord, int shape)
// @des:
// @input:
// @output:
{
	this ->kernel_ = cvCreateStructuringElementEx(cols, rows, x_coord, y_coord, shape);
}

		
void Blob::cancelNoise(int morphology, int smooth)		// Before this method is called, "setKernel" must first be set!
// @des:
// @input:
// @output:
{
	cvMorphologyEx(this ->h_, this ->h_, NULL, this ->kernel_, morphology);	// Default to opening operating	
	cvSmooth(this ->h_, this ->h_, smooth);	// Default to gaussian
}

IplImage* Blob::getHue()
// @des:
// @input:
// @output:
{
	return this ->h_;
}

IplImage* Blob::getImg()
{
	return this ->img_;
}